//
//  HudCode.m
//  storeapp
//
//  Created by huangyi on 13-2-7.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "HudTask.h"

@interface HudTask ()

@end

@implementation HudTask

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+(HudTask*)hudWithText:(NSString*)text code:(dispatch_block_t)code{
    HudTask *hud=[[HudTask alloc] init];
    hud.textString=text;
    hud.codeBlock=code;
    return hud;
}

-(void)buildHud{
    CGFloat width=120;
    CGFloat spacing=10;
    CGFloat radius=6;
    UIFont *font=UIFontFromString(@"@16");

    UIView *container=[[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:container];
    self.container=container;
    container.clipsToBounds=YES;
    container.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];
    container.layer.cornerRadius=radius;
    container.layer.shadowRadius=3;
    container.layer.shadowOpacity=1;
    container.layer.shadowOffset=CGSizeZero;
    container.layer.shadowColor=[UIColor blackColor].CGColor;
    container.autoresizingMask=(UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin);

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [container addSubview:activityView];
    [activityView startAnimating];
    width=MAX(width, CGRectGetWidth(activityView.frame));

    UILabel *textView=[[UILabel alloc] initWithFrame:CGRectZero];
    [container addSubview:textView];
    if (self.textString) {
        textView.text=self.textString;
        textView.font=font;
        textView.numberOfLines=0;
        textView.backgroundColor=[UIColor clearColor];
        [textView sizeToFit];
    }
    width=MAX(width, CGRectGetWidth(textView.frame));

    width=spacing+width+spacing;
    activityView.frame=CGRectOffset(activityView.frame, 0, spacing);
    activityView.center=CGPointMake(width/2, activityView.center.y);
    textView.frame=CGRectOffset(textView.frame, 0, CGRectGetMaxY(activityView.frame)+spacing);
    textView.center=CGPointMake(width/2, textView.center.y);
    container.frame=CGRectMake(0, 0, width, CGRectGetMaxY(textView.frame)+spacing);
    container.center=CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetHeight(self.view.frame)/2);

    [self runTask];
}

-(void)runTask{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    });
    dispatch_queue_t queue=[HudMgr shared].queue;
    dispatch_async(queue, ^{
        if (self.codeBlock) {
            self.codeBlock();
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hide];
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        });
    });
}

-(void)show{
    [super show];
}
-(void)hide{
    [super hide];
}

@end
